<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Chart extends CI_Controller {	
	
	
	
	public function index($kap='')
	{
		$var['title']	= 'Surat Keluar';
		$var['kap']		= $kap;
		//$no = $this->session->userdata('no_surat');
		//$kl = $this->session->userdata('klien_surat');
		//$ta = $this->session->userdata('tgl_surat_awal');
		//$tk = $this->session->userdata('tgl_surat_akhir');
		$var['surats']	= '';
		//$var['content'] = 'index-surat';
		$this->load->view('index',$var);
	}
	
	public function do_chart(){
		
		$user1='MKI';
		$user2='NSI';
		$user3='PRO';
		$user4='AUL';
		
		$arrData2[0][1] = $user1;
		$arrData2[1][1] = $user2;
		$arrData2[2][1] = $user3;
		$arrData2[3][1] = $user4;
		  
		$index=rand(1,2);
		
		//$Q=$this->db->get('revenue');
		//$row=$Q->row_array();
		
		//Oktober
		
		//MKI
		//if(($row['month']=1) && ($row['user_name']=$user1) )
		$this->db->where(array('month'=>1,'user_name'=>$user1));
		$Q=$this->db->get('revenue');
		$rec=$Q->row_array();
		$arrData2[0][2] = $rec['total'];
		
		//NSI
		//if(($row['month']=1) && ($row['user_name']=$user2) )
		$this->db->where(array('month'=>1,'user_name'=>$user2));
		$Q=$this->db->get('revenue');
		$rec=$Q->row_array();
		$arrData2[1][2] = $rec['total'];
		
		//PRO
		//if(($row['month']=1) && ($row['user_name']=$user3) )
		$this->db->where(array('month'=>1,'user_name'=>$user3));
		$Q=$this->db->get('revenue');
		$rec=$Q->row_array();
		$arrData2[2][2] = $rec['total'];
		
		//AUL
		//if(($row['month']=1) && ($row['user_name']=$user3) )
		$this->db->where(array('month'=>1,'user_name'=>$user4));
		$Q=$this->db->get('revenue');
		$rec=$Q->row_array();
		$arrData2[3][2] = $rec['total'];
	
		//November
		
		//MKI
		$this->db->where(array('month'=>2,'user_name'=>$user1));
		$Q=$this->db->get('revenue');
		$rec=$Q->row_array();		
		 $arrData2[0][3] = $rec['total'];
		 
		 //NSI
		 $this->db->where(array('month'=>2,'user_name'=>$user2));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[1][3] = $rec['total'];
		 
		 //PRO
		 $this->db->where(array('month'=>2,'user_name'=>$user3));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[2][3] = $rec['total'];
		 
		 //AUL
		 $this->db->where(array('month'=>2,'user_name'=>$user4));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[3][3] = $rec['total'];
	 
		 //Desember
		 //MKI
		 $this->db->where(array('month'=>3,'user_name'=>$user1));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();		
		 $arrData2[0][4] = $rec['total'];
		 
		 //NSI
		 $this->db->where(array('month'=>3,'user_name'=>$user2));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[1][4] = $rec['total'];
		 
		 //PRO
		 $this->db->where(array('month'=>3,'user_name'=>$user3));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[2][4] = $rec['total'];
		 
		 //AUL
		 $this->db->where(array('month'=>3,'user_name'=>$user4));
		 $Q=$this->db->get('revenue');
		 $rec=$Q->row_array();	
		 $arrData2[3][4] = $rec['total'];
		 
		 //Triwulan
		 $arrData2[0][5] = $arrData2[0][1]+$arrData2[0][2]+$arrData2[0][3]+$arrData2[0][4];
		 $arrData2[1][5] = $arrData2[1][1]+$arrData2[1][2]+$arrData2[1][3]+$arrData2[1][4];;
		 $arrData2[2][5] = $arrData2[2][1]+$arrData2[2][2]+$arrData2[2][3]+$arrData2[2][4];;
		 $arrData2[3][5] = $arrData2[3][1]+$arrData2[3][2]+$arrData2[3][3]+$arrData2[3][4];;
		
	 	
		  $xmlData = "<graph bgColor='#FFFFFF' caption='Revenue'  animation='1'  canvasBorderColor='FFFFFF' showBorder='0' xAxisName='Time' yAxisName='Total' showNames='1' decimalPrecision='0' formatNumberScale='0'>";  
		  $animation="<style name='CanvasAnim' type='animation' param='_xScale' start='5' duration='2' />";
		  $strCategories = "<categories>";
		  $strDataCurr = "<dataset seriesName='Oktober'>";
		  $strDataPrev = "<dataset seriesName='November'>";
		  $strDataNext = "<dataset seriesName='Desember'>";
		  $strDataBeep = "<dataset seriesName='Quarter 4'>";	
	  
	 
		  foreach ($arrData2 as $arSubData) {
			$strCategories .= "<category name='" . $arSubData[1] . "' />";
			$strDataCurr .= "<set value='" . $arSubData[2] . "' />";
			$strDataPrev .= "<set value='" . $arSubData[3] . "' />";
			$strDataNext .= "<set value='" . $arSubData[4] . "' />";
			$strDataBeep .= "<set value='" . $arSubData[5] . "' />";
			
		  }	
		  
		  $strCategories .= "</categories>";
		  $strDataCurr .= "</dataset>";
		  $strDataPrev .= "</dataset>";
		  $strDataNext .= "</dataset>";
		  $strDataBeep .= "</dataset>";	
		  $xmlData .= $animation.$strCategories . $strDataCurr . $strDataPrev .$strDataNext . $strDataBeep . "</graph>"; 
		  //mysql_query("INSERT INTO price_fluctuation (update_datetime, price) VALUES (now(), " . rand(1000, 5000) . ")", $dbConn);  
		   
		  echo $xmlData;
	}
	
	function do_chart2(){
		
		$user1='fifi';
		$user2='SAM';
		
		$call=1;
		$pers=3;
		
		//Call , $call
		$arrData2[0][1] = $user1;
		$arrData2[1][1] = $user2;
	  
	  
		//Presentation $pers
		$arrData2[2][1] = $user1;
		$arrData2[3][1] = $user2;
		  
	  
		//Minggu 1 , Call $call , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('(@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1)='=>'1','activity_id'=>$call,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[0][2] = $rec['total'];
		echo $this->db->last_query();
		
		//Minggu 1 , Call $call , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'1','activity_id'=>$call,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();	
		$arrData2[1][2] = $rec['total'];
	    //echo $this->db->last_query();
	   
		//Minggu 1 , Persentase $pers , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'1','activity_id'=>$pers,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[2][2] = $rec['total'];
		
		//Minggu 1 , Persentase $pers , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'1','activity_id'=>$pers,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[3][2] = $rec['total'];
	
		//Minggu 2 , Call $call , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'2','activity_id'=>$call,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[0][3] = $rec['total'];
		//echo $this->db->last_query();
		
		//Minggu 2 , Call $call , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'2','activity_id'=>$call,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[1][3] = $rec['total'];
	 
		//Minggu 2 , Persentase $pers , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'2','activity_id'=>$pers,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[2][3] = $rec['total'];
		
		//Minggu 2 , Persentase $pers , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'2','activity_id'=>$pers,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[3][3] = $rec['total'];
	 
		//Minggu 3 , Call $call , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'3','activity_id'=>$call,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[0][4] = $rec['total'];
		
		//Minggu 3 , Call $call , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'3','activity_id'=>$call,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();		
		$arrData2[1][4] = $rec['total'];
	 
		//Minggu 3 , Persentase $pers , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'3','activity_id'=>$pers,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[2][4] = $rec['total'];
		
		//Minggu 3 , Persentase $pers , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'3','activity_id'=>$pers,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[3][4] = $rec['total'];
	 
		 //Minggu 4 , Call $call , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'4','activity_id'=>$call,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();	
		$arrData2[0][5] = $rec['total'];
		 
		 //Minggu 4 , Call $call , $user2
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'4','activity_id'=>$call,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[1][5] =$rec['total'];
		 
		 //Minggu 3 , Persentase $pers , $user1
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'4','activity_id'=>$pers,'pic'=>$user1));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[2][5] = $rec['total'];
		
		$this->db->select('COUNT(*) as total');
		$this->db->where(array('@wd:=WEEK(activity_date,5)- WEEK(DATE_SUB(activity_date, INTERVAL DAYOFMONTH(activity_date) - 1 DAY), 5) + 1='=>'4','activity_id'=>$pers,'pic'=>$user2));
		$this->db->where('MONTH(activity_date)=MONTH(CURDATE())');
		$Q=$this->db->get('activity_transaction');
		$rec=$Q->row_array();
		$arrData2[3][5] = $rec['total'];
		
	 	
		  $xmlData = "<graph bgColor='#FFFFFF' caption='Call & Presentation'  animation='1'  canvasBorderColor='FFFFFF' showBorder='0' xAxisName='Presentation                                                          Call A Client ' yAxisName='Total' showNames='1' decimalPrecision='0' formatNumberScale='0'>";  
		  $animation="<style name='CanvasAnim' type='animation' param='_xScale' start='5' duration='2' />";
		  $strCategories = "<categories>";
		  $strDataCurr = "<dataset seriesName='Minggu I'>";
		  $strDataPrev = "<dataset seriesName='Minggu II'>";
		  $strDataNext = "<dataset seriesName='Minggu III'>";
		  $strDataBeep = "<dataset seriesName='Minggu IV'>";	
	  
	 
		  foreach ($arrData2 as $arSubData) {
			$strCategories .= "<category name='" . $arSubData[1] . "' />";
			$strDataCurr .= "<set title='test' value='" . $arSubData[2] . "' />";
			$strDataPrev .= "<set value='" . $arSubData[3] . "' />";
			$strDataNext .= "<set value='" . $arSubData[4] . "' />";
			$strDataBeep .= "<set value='" . $arSubData[5] . "' />";
			
		  }	
	  
		  $strCategories .= "</categories>";
		  $strDataCurr .= "</dataset>";
		  $strDataPrev .= "</dataset>";
		  $strDataNext .= "</dataset>";
		  $strDataBeep .= "</dataset>";	
		  $xmlData .= $animation.$strCategories . $strDataCurr . $strDataPrev .$strDataNext . $strDataBeep . "</graph>"; 
		  //mysql_query("INSERT INTO price_fluctuation (update_datetime, price) VALUES (now(), " . rand(1000, 5000) . ")", $dbConn);  
		   
		  echo $xmlData;
	}
	
	
	
}
